<% layout('/layouts/default.html', {title: '考核自评查看', libs: ['dataGrid','fileupload','validate']}){ %>
<style>
    .title-right {
        display: inline;
        float: right;
        overflow: hidden;
        margin: 0px 5px
    }
</style>
<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div style="float: left;">
                <button class="btn btn-default btn-sm" style="font-size: 16px" onclick="js.closeCurrentTabPage()" >返回</button>
            </div>
            <div style="float: right;overflow: hidden;display: flex">
                <a href="#"  id="export"  class="btn btn-primary " style="" title="${text('导出')}"> <i class="glyphicon glyphicon-export">导 出</i></a>
            </div>
        </div>
        <div class="box-body">
            <table id="dataGrid"></table>
        </div>
        <div class="box-body">
            <table id="addOrRemoveGrid"></table>
        </div>
        <div class="box-body">
            <table style="width: 100%; height: 28px;padding: 0 2px;" class="ui-jqgrid-ftable" cellspacing="0" cellpadding="0" border="0">
                <tbody>
                <tr class="ui-widget-content footrow footrow-ltr" style="background: #e6e6e6;">
                    <th width="35px;" style="text-align: center"></th>
                    <th width="157px;" style="text-align: center">合计分数:</th>
                    <th width="378px;" style="text-align: center"></th>
                    <th width="118px" style="text-align: center">&nbsp;&nbsp;总权数:<span id="quanshu"></span> &nbsp;分</th>
                    <th width="119px;" style="text-align: center"></th>
                    <th width="110px;" style="text-align: center"></th>
                    <th width="110px;" style="text-align: center"></th>
                    <th width="135px" style="text-align: center">&nbsp;&nbsp;总得分:<span id="socre"></span> 分</th>
                    <th width="170px;" style="text-align: center"></th>
                    <th width="102px;" style="text-align: center"></th>
                </tr>
                </tbody>
            </table>
        </div>
        </div>
</div>
<% } %>
<script>
    // 初始化DataGrid对象
    $('#dataGrid').dataGrid({
        // searchForm: $("#searchForm"),
        data: ${toJson(checkIndexDataDtoPage)},
        datatype: 'local',
        columnModel: [
            {header: '${text("主键")}', name: 'id', hidden: true},
            {header: '${text("年度")}', name: 'examYear', width: 150, align: "center",hidden: true},
            {
                header: '${text("指标类型")}',
                name: 'quotaGroup',
                width: 100,
                align: "center"
                ,hidden: true,
                formatter: function(val, obj, row, act){
                    if (row.quotaClass=='1'){
                        if (row.quotaGroup =='1'){
                            return '关键指标';
                        }else if(row.quotaGroup =='2'){
                            return '单项指标';
                        }
                    }else if (row.quotaClass == '2'){
                        return '加分项指标';
                    }else if (row.quotaClass == '3'){
                        return '减分项指标';
                    }
                    return ' ';
                }
            },
            {header: '${text("类别")}', name: 'quotaType', width: 100, align: "center",
                formatter: function(val, obj, row, act){
                    return js.getDictLabel(${@DictUtils.getDictListJson('dc_quotaType')},
                        val, '${text("'+val+'")}', true);
                }, edittype:'select', editoptions:{'class':'form-control',
                    items: $.merge([{dictLabel:'&nbsp;',dictValue:''}],
                        ${@DictUtils.getDictListJson('dc_quotaType')}),
                    itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
                        $(element).select2().on("change",function(){$(this).resetValid()});
                    }
                }
            },
            {header: '${text("序号")}', name: 'sortNum', width: 100, align: "center",hidden: true},
            {header: '${text("目标内容")}', name: 'quotaContent', width: 220, align: "left"},
            {header: '${text("单位")}', name: 'quotaUnit', width: 100, align: "center",hidden: true},
            {header: '${text("权数")}', name: 'quotaWeight', width: 70, align: "center",
                formatter: function(val, obj, row, act){
                    if(val == null){
                        return 0.00;
                    }else{
                        return parseFloat(val).toFixed(2);
                    }
                }
            },
            {header: '${text("本年度目标")}', name: 'currentYaerGoal', width: 70, align: "center"},
            {header: '${text("考核方式")}', name: 'assessMethod', width: 65, align: "center",
                formatter: function(val, obj, row, act){
                    return js.getDictLabel(${@DictUtils.getDictListJson('dc_exam_assessment')},
                        val, '', true);
                }
            },
            {header: '${text("百分比得分")}', name: 'percentage', width: 65, align: "center",
                formatter: function(val, obj, row, act){
                    if (val==''||typeof val=="undefined"){val=0;}
                    return parseFloat(val).toFixed(2)+"%";
                }
            },
            {header: '${text("分数")}', name: 'quotaScore', width: 80, align: "center",
                formatter: function(val, obj, row, act){
                    return Number(val).toFixed(3);
                }},
            {header: '${text("备注")}', name: 'scoreReason', width: 100, align: "center"},
            {header: '${text("排名")}', name: 'ranking', width: 60, align: "center"},
        ],
        editGrid: false,
        // 是否显示行号
        showRownum: true,
        // 列表是否允许排序
        sortableColumn: false,
        // 是否显示底部合计行
        showFooter: true,
        // 表格内没有数据的时候是否提示 “无数据显示”
        emptyDataHint: true,
        // 加载成功后执行事件
        ajaxSuccess: function (data) {

        }
    });


    $('#addOrRemoveGrid').dataGrid({
        // searchForm: $("#searchForm"),
        data: ${toJson(addAndSub)},
        datatype: 'local',
        columnModel: [
            {header: '${text("主键")}', name: 'id', hidden: true},
            {header: '${text("年度")}', name: 'examYear', width: 150, align: "center",hidden: true},
            {
                header: '${text("指标类型")}',
                name: 'quotaGroup',
                width: 100,
                align: "center",
                hidden: true,
                formatter: function(val, obj, row, act){
                    if (row.quotaClass=='1'){
                        if (row.quotaGroup =='1'){
                            return '关键指标';
                        }else if(row.quotaGroup =='2'){
                            return '单项指标';
                        }
                    }else if (row.quotaClass == '2'){
                        return '加分项指标';
                    }else if (row.quotaClass == '3'){
                        return '减分项指标';
                    }
                    return ' ';
                }
            },
            {header: '${text("类别")}', name: 'quotaType', width: 100, align: "center",
                formatter: function(val, obj, row, act){
                    return js.getDictLabel(${@DictUtils.getDictListJson('dc_quotaType')},
                        val, '${text("'+val+'")}', true);
                }, edittype:'select', editoptions:{'class':'form-control',
                    items: $.merge([{dictLabel:'&nbsp;',dictValue:''}],
                        ${@DictUtils.getDictListJson('dc_quotaType')}),
                    itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
                        $(element).select2().on("change",function(){$(this).resetValid()});
                    }
                }
            },
            {header: '${text("序号")}', name: 'sortNum', width: 100, align: "center",hidden: true},
            {header: '${text("目标内容")}', name: 'quotaContent', width: 220, align: "left"},
            {header: '${text("单位")}', name: 'quotaUnit', width: 100, align: "center",hidden: true},
            {header: '${text("权数")}', name: 'quotaWeight', width: 70, align: "center",
                formatter: function(val, obj, row, act){
                    if(val == null){
                        return 0.00;
                    }else{
                        return parseFloat(val).toFixed(2);
                    }
                }
            },
            {header: '${text("本年度目标")}', name: 'currentYaerGoal', width: 70, align: "center"},
            {header: '${text("考核方式")}', name: 'assessMethod', width: 65, align: "center",
                formatter: function(val, obj, row, act){
                    return js.getDictLabel(${@DictUtils.getDictListJson('dc_exam_assessment')},
                        val, '', true);
                }
            },
            {header: '${text("百分比得分")}', name: 'percentage', width: 65, align: "center",
                formatter: function(val, obj, row, act){
                    if (val==''||typeof val=="undefined"){val=0;}
                    return parseFloat(val).toFixed(2)+"%";
                }
            },
            {header: '${text("分数")}', name: 'quotaScore', width: 80, align: "center",
                formatter: function(val, obj, row, act){
                    return Number(val).toFixed(3);
                }},
            {header: '${text("备注")}', name: 'scoreReason', width: 100, align: "center"},
            {header: '${text("操作")}', width: 60, align: "center"}
        ],
        editGrid: false,
        // 是否显示行号
        showRownum: true,
        // 列表是否允许排序
        sortableColumn: false,
        // 是否显示底部合计行
        showFooter: true,
        // 表格内没有数据的时候是否提示 “无数据显示”
        emptyDataHint: true,
        // 加载成功后执行事件
        ajaxSuccess: function (data) {
            refreshZpCount();
        }
    });



    function refreshZpCount(){
        let data = $('#dataGrid').dataGrid('getRowData');
        let allQuotaWeight=0;
        let allQuotaScore=0;
        for (let i = 0; i < data.length; i++) {
            if (data[i].quotaGroup == '减分项指标'){
                allQuotaWeight-=parseFloat(data[i].quotaWeight||0);
                allQuotaScore-=parseFloat(data[i].quotaScore||0);
            }else{
                allQuotaWeight+=parseFloat(data[i].quotaWeight||0);
                allQuotaScore+=parseFloat(data[i].quotaScore||0);
            }
        }
        $('#dataGrid').dataGrid("footerData", "set", {
            "quotaType" : "<center><span>汇总：</span></center>",
            "quotaWeight" : "<span>总权数:"+Number(allQuotaWeight).toFixed(2)+" &nbsp;</span>分",
            "quotaScore": "<span>得分:"+Number(allQuotaScore).toFixed(3)+" &nbsp;</span>分"
        }, false);

        let addRemovedata = $('#addOrRemoveGrid').dataGrid('getRowData');
        let addRemoveQuotaWeight=0.0;
        let addRemoveQuotaScore=0.0;
        for (let i = 0; i < addRemovedata.length; i++) {
            let quotaClass= addRemovedata[i].quotaGroup;
            if (quotaClass=="加分项指标"){
                addRemoveQuotaScore=accAdd(parseFloat(addRemovedata[i].quotaScore||0),parseFloat(addRemoveQuotaScore));
            }else if (quotaClass=="减分项指标"){
                addRemoveQuotaScore=accSub(parseFloat(addRemoveQuotaScore),Math.abs(parseFloat(addRemovedata[i].quotaScore||0)));
            }
            addRemoveQuotaWeight=accAdd(parseFloat(addRemovedata[i].quotaWeight||0),addRemoveQuotaWeight);
        }
        $('#addOrRemoveGrid').dataGrid("footerData", "set", {
            "quotaType" : "<center><span>汇总：</span></center>",
            "quotaWeight" : "<span>总权数:"+parseFloat(addRemoveQuotaWeight).toFixed(2)+" &nbsp;</span>分",
            "quotaScore": "<span>得分:"+parseFloat(addRemoveQuotaScore).toFixed(3)+" &nbsp;</span>分"
        }, false);

        // $('#bottom').dataGrid("footerData", "set", {
        //     "examYear" : "<center><span>合计分数：</span></center>",
        //     "quotaWeight" : "总权数<span>&nbsp;"+accAdd(parseFloat(addRemoveQuotaWeight),parseFloat(allQuotaWeight)).toFixed(3)+" &nbsp;</span>分",
        //     "quotaScore": "总分<span>&nbsp;"+accAdd(parseFloat(addRemoveQuotaScore),parseFloat(allQuotaScore)).toFixed(3)+" &nbsp;</span>分"
        // }, false);
        $("#socre").text(accAdd(parseFloat(addRemoveQuotaScore),parseFloat(allQuotaScore)).toFixed(3));
        $("#quanshu").text(accAdd(parseFloat(addRemoveQuotaWeight),parseFloat(allQuotaWeight)).toFixed(2));
    }

    function accAdd(arg1,arg2){
        var r1,r2,m;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2))
        return (arg1*m+arg2*m)/m
    }

    function accSub(arg1,arg2){
        var r1,r2,m,n;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        //last modify by deeka
        //动态控制精度长度
        n=(r1>=r2)?r1:r2;
        return ((arg1*m-arg2*m)/m).toFixed(n);
    }

    function getDate(){
        let now=new Date()
        var date2= now.toLocaleDateString().split("/");
        var result="";
        for (let key in date2){
            if (key == 1){
                if (date2[key].length < 2){
                    date2[key] = "0"+date2[key];
                }
            }
            result=result+date2[key];
        }
        var timeString = now.toLocaleTimeString().replace(":","").replace(":","").substring(0,4);
        return result+timeString;
    }

   var DATA_= ${toJson(checkIndexDataDtoPage)};
    $("#export").click(function (){
        var times= getDate();
        let xhr = new XMLHttpRequest();
        var url_="${ctx}/exam/dcSupExamSumScoreResult/export?examId="+DATA_[0].examId;
        xhr.open('GET', url_, true);
        xhr.setRequestHeader('Content-Type', `application/json`);
        xhr.responseType = 'blob';
        let that = this;
        xhr.onload = function () {
            if (this.status == 200) {
                //接受二进制文件流
                var blob = this.response;
                let downloadElement = document.createElement('a');
                let href = blob;
                if (typeof blob == 'string') {
                    downloadElement.target = '_blank';
                } else {
                    href = window.URL.createObjectURL(blob) ;//创建下载的链接
                }
                downloadElement.href = href;
                //下载后文件名
                downloadElement.download = "考核得分"+times.substring(0,times.length-4)+".xls";
                document.body.appendChild(downloadElement)
                downloadElement.click(); //点击下载
                document.body.removeChild(downloadElement); //下载完成移除元素
                if (typeof blob != 'string') {
                    window.URL.revokeObjectURL(href); //释放掉blob对象
                }
            }else{
                js.showMessage("文件导出失败");
            }
        }
        xhr.send();
    })
</script>